import Vue from 'vue'
import Router from 'vue-router'
import store from "../store";
import HelloWorld from '@/components/HelloWorld'
import List from "../components/List";
import Login from "../components/Login";
Vue.use(Router)

const router =  new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/list',
      name: 'List',
      component: List,
      meta:{
        // 只有登录过后才可以访问
        requireLogin:true
      }
    },
    {
      path:"/login",
      name:"Login",
      component:Login,
      meta:{
        redirectIndex:true
      }
    }
  ]
})
router.beforeEach((to,from,next)=>{
  if (to.meta.requireLogin && !store.state.user.isLogin) {
    next({name:'Login'})
  }else if(to.meta.redirectIndex && store.state.user.isLogin){
    next('/')
  } else{
    next()
  }
})
export default router